/**
 * 
 */
package game;

import javax.vecmath.Vector3f;
import org.lwjgl.input.Keyboard;
import AGFX.F3D.F3D;
import AGFX.F3D.AppWrapper.TF3D_AppWrapper;
import AGFX.F3D.Body.TF3D_Body;
import AGFX.F3D.Camera.TF3D_Camera;
import AGFX.F3D.Config.TF3D_Config;
import AGFX.F3D.Light.TF3D_Light;
import AGFX.F3D.Model.TF3D_Model;
import AGFX.F3D.Skybox.TF3D_Skybox;

/**
 * @author Baskervil
 * 
 */
public class drifter3d extends TF3D_AppWrapper
{

	public TF3D_Camera      Camera;
	public TF3D_Body        landscape;
	public Vehicle 	        car;
	public TF3D_Body        PSphere;
	public TGrid			grid;
	public TEditor 			editor;
	public TF3D_Model 		model;
	public TF3D_Model 		cone;
	public TF3D_Model 		sipka;
	public Vector3f			ed_pos;
	private int i = 9;
	
	private int look = 1;


	public drifter3d()
	{

	}

	@Override
	public void onConfigure()
	{
		try
		{
			// Redefine Config
			F3D.Config = new TF3D_Config();
			//F3D.Config.io_preload_source_mode = "PRELOAD_FROM_JAR";
			F3D.Config.Load("config/drifter.cfg");
			super.onConfigure();

		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	@Override
	public void onInitialize()
	{
		F3D.Meshes.Add("abstract::kolesoL.a3da");
		F3D.Meshes.Add("abstract::kolesoR.a3da");
		F3D.Meshes.Add("abstract::airport.a3da");
		F3D.Meshes.Add("abstract::grid.a3da");
		F3D.Meshes.Add("abstract::cone.a3da");
		F3D.Meshes.Add("abstract::sipka.a3da");
		F3D.Meshes.Add("abstract::karoseria.a3da");
		
		this.editor = new TEditor();
		
		F3D.Worlds.CreateWorld("GAME");

		this.Camera = new TF3D_Camera("Game_Camera");
		this.Camera.SetPosition(0.0f, 15.0f, -30.0f);
		this.Camera.SetRotation(0, 180, 0);
		this.Camera.ctype = F3D.CAMERA_TYPE_TARGET;

		F3D.Cameras.Add(this.Camera);
		F3D.Worlds.SetCamera(this.Camera);

		this.Camera.Sky = new TF3D_Skybox();

		// Add light to scene
		TF3D_Light light = new TF3D_Light("light_0", 0);
		light.SetPosition(3, 3, 3);
		light.spot_constant_att = 0.001f;
		light.Enable();

		this.landscape = new TF3D_Body("GAME_PLANE");
		this.landscape.AssignMesh("abstract::airport.a3da");
		this.landscape.Enable();
		//this.landscape.SetScale(0.5f, 0.5f, 0.5f);
		this.landscape.SetPosition(1, 0, 0);
		this.landscape.CreateRigidBody(F3D.BULLET_SHAPE_TRIMESH, 0.0f);
		
		
		

		
		this.cone = new TF3D_Model("CONE");
		this.cone.AssignMesh("abstract::cone.a3da");
		//this.landscape.Enable();
		//this.landscape.SetPosition(0,0.2f,10);
		//this.landscape.CreateRigidBody(F3D.BULLET_SHAPE_CYLINDER, 1.0f);

		this.sipka = new TF3D_Model("SIPKA");
		this.sipka.AssignMesh("abstract::sipka.a3da");
		this.sipka.SetRotation(90, 0,0);

		this.car = new Vehicle("CAR_01");

		this.car.SetRotation(0, 0, 0);
		this.car.model_chassis = F3D.Meshes.FindByName("abstract::karoseria.a3da");
		this.car.model_wheel_FL = F3D.Meshes.FindByName("abstract::kolesoL.a3da");
		this.car.model_wheel_FR = F3D.Meshes.FindByName("abstract::kolesoR.a3da");
		this.car.model_wheel_BL = F3D.Meshes.FindByName("abstract::kolesoL.a3da");
		this.car.model_wheel_BR = F3D.Meshes.FindByName("abstract::kolesoR.a3da");
		

		this.car.SetPosition(0, 0.7f, 0);

		this.car.Create();
		

	}

	@Override
	public void onUpdate3D()
	{
		F3D.Draw.Axis(2.0f);
		/*
		if (F3D.Worlds.camera.name == "Editor_Camera"){
			this.editor.grid.drawGrid(10);
			RigidBody pick = F3D.Pick.Mouse(Mouse.getX(),Mouse.getY());
			if (pick != null){
				this.ed_pos = F3D.Pick.rayCallback.hitPointWorld;
				this.editor.cursor.SetPosition(ed_pos.x,0.5f,ed_pos.z);
			}
		}*/
		// look at car
		// F3D.Cameras.items.get(F3D.Cameras.CurrentCameraID).TargetPoint.set(this.car.GetPosition());

		// update AXIS orientation
		this.car.UpdateAxisDirection();

		// get car position
		Vector3f car_pos = new Vector3f(this.car.GetPosition());
		Vector3f pow_pos = new Vector3f(this.car.GetPosition());
		Vector3f eng_pos = new Vector3f(this.car.GetPosition());
		// get car forward direction
		Vector3f car_dir = new Vector3f(this.car.axis._forward);

		
     
		
		if (Keyboard.isKeyDown(Keyboard.KEY_UP))
		{
			Vector3f ca_dir = new Vector3f(this.car.axis._forward);
			ca_dir.scale(8800);
			pow_pos.add(ca_dir);
			Vector3f c_dir = new Vector3f(this.car.axis._forward);
			c_dir.scale(-1.5f);
			c_dir.normalize();
			eng_pos.add(c_dir);
			

			
			//this.car.gEngineForce = 200f * this.car.maxEngineForce * F3D.Timer.AppSpeed();
			//this.car.RB_carChassis.applyForce(pow_pos,eng_pos);
			this.car.RB_carChassis.applyForce(pow_pos,c_dir);
			this.cone.SetPosition(eng_pos.x,1.2f,eng_pos.z);
			this.sipka.SetPosition(eng_pos.x,0,eng_pos.z);
			this.sipka.SetRotation(c_dir);
			this.car.gBreakingForce = 0.f;

		} else
		{ 
			this.car.gEngineForce = 0f;
		}
		
		if (Keyboard.isKeyDown(Keyboard.KEY_M))
		{
			if (this.i < 12){
				this.car.material_chassis = this.i;
				this.i++;
			} else {
				this.i = 9;
			}
			
		} 
		
		if (Keyboard.isKeyDown(Keyboard.KEY_SPACE))
		{
			this.car.gBreakingForce = 200f * this.car.maxBreakingForce * F3D.Timer.AppSpeed();
			this.car.gEngineForce = 0.f;
					
			} 
		
		//F3D.Log.info("speed", ""+this.car.RB_carChassis.getLinearVelocity(new Vector3f(0,0,0)).z);
		//this.car.wheelFriction = this.car.RB_carChassis.getLinearVelocity(new Vector3f(0,0,0)).z*10;
		

		// reset dir height
		car_dir.y = 0;
		//car_dir.x /= 2;
	
		// normalize
		car_dir.normalize();

		
		if (Keyboard.isKeyDown(Keyboard.KEY_1))
		{
			this.look = 1;
		} 
		
		if (Keyboard.isKeyDown(Keyboard.KEY_2))
		{
			this.look = 2;
		} 
		
		if (this.look == 1)
		{
			car_dir.scale(-5f);
			car_pos.add(car_dir);
			car_pos.y += 2f;
			//car_pos.x += -4;
		} 
		
		if (this.look == 2)
		{
			car_dir.scale(0f);
			car_pos.add(car_dir);
			car_pos.y += 1f;
			car_pos.x += -4;
		} 
		
		if (Keyboard.isKeyDown(Keyboard.KEY_3))
		{
			F3D.Worlds.SetWorld("GAME");
			F3D.Worlds.SetCamera(F3D.Cameras.GetCamera("Game_Camera"));
			//F3D.Cameras.CurrentCameraID = F3D.Cameras.FindByName("Game_Camera");
			//F3D.Viewport.Clear(0f, 0f, 0f, 1f);	
		}
		
		if (Keyboard.isKeyDown(Keyboard.KEY_4))
		{
			F3D.Worlds.SetWorld("EDITOR");
			F3D.Worlds.SetCamera(F3D.Cameras.GetCamera("Editor_Camera"));
			//F3D.Cameras.CurrentCameraID = F3D.Cameras.FindByName("Editor_Camera");
			//F3D.Viewport.Clear(0f, 0f, 0f, 1f);	
		}
		
		if (Keyboard.isKeyDown(Keyboard.KEY_LEFT))
		{
			this.car.gVehicleSteering += this.car.steeringIncrement;
			if (this.car.gVehicleSteering > this.car.steeringClamp)
			{
				this.car.gVehicleSteering = this.car.steeringClamp;
				//car_dir.x = this.car.steeringClamp;
			}

		} else if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
		{
			this.car.gVehicleSteering -= this.car.steeringIncrement;
			if (this.car.gVehicleSteering < -this.car.steeringClamp)
			{
				this.car.gVehicleSteering = -this.car.steeringClamp;
				//car_dir.x = -this.car.steeringClamp;
			}

		} else
		{
			this.car.gVehicleSteering = 0f;
		}

		if (Keyboard.isKeyDown(Keyboard.KEY_R))
		{
			this.car.Reset();
		}

		
		// set camera position
		this.Camera.SetPosition(car_pos);

		// set camera target point
		this.Camera.TargetPoint.set(this.car.GetPosition());
		


	}

	@Override
	public void onUpdate2D()
	{
		F3D.Viewport.DrawInfo(0, 0);
		//this.grid.drawGrid();
	
	}

	@Override
	public void OnDestroy()
	{

	}

	public static void main(String[] args)
	{
		new drifter3d().Execute();
		System.exit(0);
	}

}
